<?php
/***************************************************************************
 * 
 * Copyright (c) 2010 , Inc. All Rights Reserved
 * 
 **************************************************************************/
 
 
 
/**
 * @file XmlUtil.php
 * @author mall(com@)
 * @date 2010/08/02 17:46:47
 * @brief 
 *  
 **/

final class XmlUtil
{
    static $search = array('&','>','<','"',"'");
    static $replace = array('&amp;','&gt;','&lt;','&quot;','&apos;');
    static private function _innerArray2Xml($arr,&$ret,$level)
    {
        foreach($arr as $key=>$value) {
            if (is_numeric($key)) {
                $key = 'item';
            }
            if (is_array($value)) {
                $ret[] = str_repeat("\t",$level-1);
                $ret[] = "<$key>\r\n";
                self::_innerArray2Xml($value,$ret,$level+1);
                $ret[] = str_repeat("\t",$level-1);
                $ret[] = "</$key>\r\n";
            } else {
                $value = str_replace(self::$search,self::$replace,$value);
                $ret[] = str_repeat("\t",$level-1);
                $ret[] = "<$key>$value</$key>\r\n";
            }
        } 
    }

    static function array2Xml(Array $arr,$encoding)
    {
        $ret = array("<?xml version=\"1.0\" encoding=\"$encoding\" ?>\r\n");
        if ($arr) {
            $data = array('HapN'=>$arr);
            self::_innerArray2Xml($data,$ret,1);
        }
        return implode('',$ret);
    }

    static function xml2Array($xmlstr)
    {
        //TODO
    }
}





/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */
